// pages/login/login.js
const md5 = require('./md5.js');
const util = require('../../utils/util.js');
const wxPromisify = util.wxPromisify;
const wx_request_promisify = wxPromisify(wx.request);
const appid ='wx5ceba01503f23207';
let prefix = 'https://gr.tunnel.qydev.com/demo';
var app = getApp();
Page({
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    var that = this;
    wx.login({
      success: function (res) {
        //微信js_code
        console.info("wxcode:" + res.code);
        that.setData({ wxcode: res.code });
        //获取用户信息
        wx.getUserInfo({
          success: function (res) {
            //获取用户敏感数据密文和偏移向量
            console.info("encryptedData:" + res.encryptedData);
            console.info("iv:" + res.iv);
            that.setData({ encryptedData: res.encryptedData })
            that.setData({ iv: res.iv })
            //获取用户thirdSessionId
            wx_request_promisify({
              url: prefix + '/components/wxapplogin/open/isAccountExistedByWxapp.json',
              method: 'POST',
              data: {
                code: that.data.wxcode,
                encryptedData: res.encryptedData,
                iv: res.iv,
                appId: appid
              },
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
            }).then(res => {
              console.info(res.data);
              console.info(res.data.data);
              wx.setStorageSync('thirdSessionId',res.data.data.thirdSessionId);
              //已经绑定过账号直接进行登录
              if (res.data.data.existed){
                wx_request_promisify({
                  url: prefix + '/login',
                  header: {
                    'content-type': 'application/x-www-form-urlencoded',
                    'Cookie': 'JSESSIONID=' + wx.getStorageSync('thirdSessionId')
                  },
                  method: 'POST',
                  data: {
                    username: res.data.data.userInfo.uid,
                    password: "",
                    appId: appid,
                    login_ct: 'ajax',
                    client:'wxapp'
                  }
                }).then(res => {
                  console.info(res);
                  if (res.data.success) {
                    wx.setStorageSync('JSESSIONID', res.data.data.JSESSIONID);

                            wx.showToast({
                              title: '成功',
                              icon: 'success',
                              duration: 2000,
                              success: function (e) {
                                wx.redirectTo({
                                  url: '/pages/clazz/clazz'
                                });
                              }
                            });

                  } else {
                    wx.showToast({
                      title: '登录失败',
                      icon: 'warn',
                      duration: 2000
                    })
                  }
                }).catch(e => {
                  wx.showModal({
                    title: '登录失败2',
                    success: function (res) {
                      if (res.confirm) {

                      } else if (res.cancel) {

                      }
                    }
                  })
                })
              }
            }).catch();
          }
        })
      }
    });

  },
  onReady: function () {

  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
  userInfo(res) {
    if (res.currentTarget.id === 'user_name') {
      this.setData({ phonenumber: res.detail.value })
    } else if (res.currentTarget.id === 'password') {
      this.setData({ password: res.detail.value })
    }
    this.data.phonenumber && this.data.password && this.setData({ isdisabled: false })
  },
  getCode() {
    wx_request_promisify({
      url: prefix + '/open/safetykey/getkey.json',
      method: 'GET'
    }).then(res => {
      return wx_request_promisify({
        url: prefix + '/plugins/sms/open/sendCaptcha.json',
        data: {
          "phonenumber": this.data.phonenumber,
          "bskeyid": res.data.data,
          "type": "1"
        },
        method: "GET"
      })
    }).then(res => {
      console.log(res)
    })
  },
  loginSubmit() {
    var that = this;
    wx_request_promisify({
      url: prefix + '/components/wxapplogin/open/autoRegistByWxapp.json',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': 'JSESSIONID=' + wx.getStorageSync('thirdSessionId')
      },
      method: 'POST',
      data: {
        username: this.data.phonenumber,
        password: md5.hexMD5(this.data.password),
        appId: appid,
        login_ct: 'ajax'
      }
    }).then(res =>{
      console.info(res);
    });
  }
})